package com.bianguo.android.baidumapdemo.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.baidu.lbsapi.tools.Point;
import com.bianguo.android.baidumapdemo.utils.MLog;

/**
 * Created by Administrator on 2018/3/28.
 */

public class BezierView extends View {

    private Path mPath;
    private Paint mPaint;
    private float startX, startY, endX, endY, touchX, touchY;
    private boolean isFill;

    public BezierView(Context context) {
        super(context);
        initView();
    }

    public BezierView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public BezierView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPath = new Path();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        startX = getMeasuredWidth() / 5;
        startY = endY = getMeasuredHeight() / 2;
        endX = getMeasuredWidth() * 4 / 5;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setStrokeWidth(10);
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(startX, startY, 10, mPaint);
        canvas.drawCircle(endX, endY, 10, mPaint);
        canvas.drawCircle(touchX, touchY, 10, mPaint);
        mPaint.setTextSize(20);
        canvas.drawText("这是一阶贝塞尔曲线,就一条直线,没什么好说的", startX, startY / 4, mPaint);

        mPaint.setColor(Color.BLUE);
        if (isFill) {
            mPaint.setStyle(Paint.Style.FILL);
        } else {
            mPaint.setStyle(Paint.Style.STROKE);
        }
        //绘制一阶
//        mPath.moveTo(startX, startY / 3);
//        mPath.lineTo(endX, endY / 3);
        mPath.moveTo(1248, 928);
        mPath.quadTo(656, 528, 64, 2336);
        canvas.drawPath(mPath, mPaint);
        mPath.reset();


        mPath.moveTo(startX, startY);
        mPath.quadTo(touchX, touchY, endX, endY);
        canvas.drawPath(mPath, mPaint);
        mPath.reset();

        mPath.moveTo(touchX + getWidth() / 20, touchY - getHeight() / 10);
        mPath.lineTo(touchX + getWidth() / 20, touchY);
        mPath.lineTo(touchX - getWidth() / 20, touchY);
        mPath.lineTo(touchX - getWidth() / 20, touchY - getHeight() / 10);
        mPath.lineTo(touchX, touchY - getHeight() / 7);
        mPath.lineTo(touchX + getWidth() / 20, touchY - getHeight() / 10 + 2);
        canvas.drawPath(mPath, mPaint);
        mPath.reset();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        touchX = event.getX();
        touchY = event.getY();
        postInvalidate();
        return true;
    }
}
